'use strict';

// todoPage controller
app.controller('warehouse', ['$state','$filter','$window','$http','$scope','$modal','toaster','$log', function($state,$filter,$window,$http,$scope,$modal,toaster,$log) {
    $scope.title = {
        title:'仓库列表',
        addTitle:'仓库信息'
    };
    $scope.toaster = {
        type: 'warning',
        title: '提示：',
        text: '内容不能为空！'
    };

    $scope.pop = function(){
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    };
    function showMessage(type,title,text){
        $scope.toaster.type = type;
        $scope.toaster.text = text;
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    }
    function checkItems(){
        var items = ['warehouseCkmc','warehouseNumber','warehouseCkwz'];
        var itemsValue = ['仓库名称','仓库编码','仓库位置'];
        for(var i=0;i<items.length;i++){
            if($('#'+items[i]+'').val()===''){
                $('#'+items[i]+'').focus();
                showMessage('warning','',itemsValue[i]+"不能为空！");
                return false;
            }
        }
        return true;
    }
    $scope.lists = [];
    $scope.mylabel = $scope.lists[0];//默认数组第一个作为option的值
    function getSelect(){
        toaster.getInfo('/a_yongan/list/allselect',null).then(function(data){
            if(data && data.data && data.data.data){
                $scope.lists = data.data.data.allSelectCklx.split(";");
            }
        });
    }
    getSelect();
    function addDo(){
        var notesdata = {};
        if(!$scope.warhouseRow.warehouseId){
            notesdata = {
                "warehouseId":"",
                "qy":$("#qy").prop("checked"),
                "zdr":$scope.app.userName,
                "zdrId":$scope.app.userId,
                "warehouseNumber":$("#warehouseNumber").val(),
                "warehouseCkmc":$("#warehouseCkmc").val(),
                "warehouseType":$scope.lists[$("#warehouseType").val()],
                "warehouseCkwz":$("#warehouseCkwz").val(),
                "warehouseBz":$("#warehouseBz").val()
            }
        }else{
            notesdata = {
                "warehouseId":$scope.warhouseRow.warehouseId,
                "qy":$("#qy").prop("checked"),
                "warehouseNumber":$("#warehouseNumber").val(),
                "warehouseCkmc":$("#warehouseCkmc").val(),
                "warehouseType":$scope.lists[$("#warehouseType").val()],
                "warehouseCkwz":$("#warehouseCkwz").val(),
                "warehouseBz":$("#warehouseBz").val()
            }
        }
        var info="";
        toaster.postInfo('/a_yongan/add/warehouse',notesdata).then(function(data){
            if(data && data.data && data.data.success){
                showMessage('seccess','','保存成功！');
                $scope.warehouseAdd = !($scope.warehouseAdd);
                $scope.warehouseList = !($scope.warehouseList);
                getList();
                return info;
            }else{
                showMessage('warning','',"保存失败！");
                info = "请求错误！";
            }
        });
        return info;
    }
    function getList(){
        toaster.getInfo('/a_yongan/list/warehouse',null).then(function(data){
            if(data && data.data && data.data.data){
                makeData(data.data.data);
            }
        });
    }
    getList();
    function makeData(data){
        data.forEach(function (item) {
            item.createTime = $filter("date")(item.createTime, "yyyy/MM/dd");
            if(item.qy=='true'){
                item.qy = true;
                item.status = '使用中...';
                item.status1 = 'label bg-info';
            }else{
                item.qy = false;
                item.status = '禁用中...';
                item.status1 = 'label bg-danger';
            }
        })
        $scope.saveDate = data;
    }
    $scope.message = '是否确定保存？';
    $scope.saveWarehouse = function (size) {
        if(!checkItems()){
            return;
        }
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            var info = addDo();

            //$state.reload('app/warehouse');
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.isshow=true;
    $scope.warehouseAdd = false;
    $scope.warehouseList = true;
    $scope.warehouseJump = function(path) {
        $scope.mylabel = $scope.lists[0];
        $scope.warhouseRow = {
            'createTime':$filter("date")(new Date(), "yyyy/MM/dd"),
            'zdr':$scope.app.userName,
            'qy':true,
            'warehouseNumber':'CK'+$filter("date")(new Date(), "yyyyMMddHHmmss")
        };
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
    };
    $scope.warhouseRow = {};
    $scope.columns = [
        {
            title:'仓库编码',
            show:true
        },
        {
            title:'仓库名称',
            show:true
        },
        {
            title:'仓库类型',
            show:true
        },
        {
            title:'创建时间',
            show:true
        },
        {
            title:'文件状态',
            show:true
        }
    ];
    $scope.showSelectDept = function(id){
        var modalInstance = $modal.open({
            templateUrl: 'selectDept.html',
            controller: 'selectDeptCtrl',
            size: 'sm',
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            $('#'+id+'').val(selectedItem.name);
            showMessage('success','','选择成功！');
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.saveDate=[];
    $scope.showInfo = function(row){
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
        $scope.warhouseRow = row;
        var isHave = false;
        $scope.lists.forEach(function(item){
            if(item === $scope.warhouseRow.warehouseType){
                isHave = true;
            }
        });
        if(!isHave){
            $scope.lists.push($scope.warhouseRow.warehouseType);
        }
        $scope.mylabel = $scope.warhouseRow.warehouseType;
    }
}])
;